package com.bailiban.aigou.admin.dao.impl;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.bailiban.aigou.admin.dao.CarouselFigureDao;
import com.bailiban.aigou.admin.entity.CarouselFigure;
import com.bailiban.aigou.admin.utils.PageModel;

@Repository
public class CarouselFigureDaoImpl implements CarouselFigureDao {
	@Autowired
	private SessionFactory sessionFactory;
	/**
	 * 查询轮播分页数据
	 */
	@Override
	public PageModel<CarouselFigure> getCarouselFigurePages(int pageNo, int pageSize) {
		    Session session = sessionFactory.getCurrentSession();
		    PageModel<CarouselFigure> pageModel=new PageModel<>();
		    pageModel.setPageNo(pageNo);
		    pageModel.setPageSize(pageSize);
		    
			Criteria criteria = session.createCriteria(CarouselFigure.class); //1：分页
			criteria.setFirstResult((pageNo-1)*pageSize);  //2 分页 规定写法 setFirstResult（页号-1*页数）
			criteria.setMaxResults(pageSize);//3规定写法  setMaxResults（页数）
			List<CarouselFigure> list = criteria.list();
			
			pageModel.setList(list);
			return pageModel;
	}
    /**
     * 查询轮播实体记录总条数
     */
	@Override
	public int getCarouselFigureCount() {
		Session session = sessionFactory.getCurrentSession();
		Query query = session.createQuery("select count(id) from CarouselFigure");
		List<Long> list = query.list();
		System.out.println("商品总条数"+list.get(0).intValue());
		return list.get(0).intValue();
	}
   /**
    * 保存轮播实体
    */
	@Override
	public void addCarouselFigure(CarouselFigure CarouselFigure) {
		sessionFactory.getCurrentSession().save(CarouselFigure);

	}
    /**
     * 根据ID查询返回出一个轮播实体
     */
	@Override
	public CarouselFigure getCarouselFigureById(String id) {
		Session session=sessionFactory.getCurrentSession();
		CarouselFigure carouselFigure=session.get(CarouselFigure.class, id);
		return carouselFigure;
	}
    /**
     * 修改轮播实体
     */
	@Override
	public void updateCarouselFigure(CarouselFigure CarouselFigure) {
		
		sessionFactory.getCurrentSession().update(CarouselFigure);
	}
   /**
    * 删除轮播实体
    */
	@Override
	public void deleteCarouselFigureById(String id) {
		Session session=sessionFactory.getCurrentSession();
		CarouselFigure carouselFigure=getCarouselFigureById(id);
		session.delete(carouselFigure);
	}
    /**
     * 获取所有轮播实体
     */
	@Override
	public List<CarouselFigure> getAllcarouselFigure() {
		Session session = sessionFactory.getCurrentSession();
		List<CarouselFigure> list=new ArrayList<CarouselFigure>();
		Query query = session.createQuery("from CarouselFigure ORDER BY sequenceNum ASC");
		list=query.list();
		return list;
	}

}
